﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Justin.WcfServices.Contracts;
using Justin.WcfServices.Services;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Hosting
{
    class Program
    {
        static void Main(string[] args)
        {
            //-----------------------------------------------------HTTP-GET 发布元数据-----------------------------------------------------

            //WCF中元数据的发布通过一个特殊的服务行为ServiceMetadataBehavior实现。在上面提供的服务寄宿代码中，
            //我们为创建的ServiceHost添加了ServiceMetadataBehavior，并采用了基于HTTP-GET的元数据获取方式，元
            //数据的发布地址通过ServiceMetadataBehavior的HttpGetUrl指定。在调用ServiceHost的Open方法对服务成
            //功寄宿后，我们可以通过该地址获取服务相关的元数据。

            //using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
            //{
            //    host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://www.wcf.com/calculatorservice");
            //    if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
            //    {
            //        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            //        behavior.HttpGetEnabled = true;
            //        behavior.HttpGetUrl = new Uri("http://www.wcf.com/calculatorservice/metadata");
            //        host.Description.Behaviors.Add(behavior);
            //    }

            //    host.Opened += delegate
            //    {
            //        Console.WriteLine("CalculaorService已经启动，按任意键终止服务！");
            //    };

            //    host.Open();
            //    Console.Read();
            //}

            //-----------------------------------------------------MEX终结点发布元数据----------------------------------------------------------

            //Uri httpBaseAddress = new Uri("http://www.wcf.com/");
            //ServiceHost host = new ServiceHost(typeof(CalculatorService), httpBaseAddress);
            //ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();

            //if (metadataBehavior == null)
            //{
            //    metadataBehavior = new ServiceMetadataBehavior();
            //    //没有必要启用 HTTP-GET 选项，但是即使启用了也不会造成影响。
            //    //metadataBehavior.HttpGetEnabled = true;
            //    host.Description.Behaviors.Add(metadataBehavior);
            //}

            //host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "");

            ////一个行为(IMetadataExchange)被添加到服务中来指导WCF包含MEX契约。
            //host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
            //host.Open();

            //Console.WriteLine("CalculaorService已经启动，按任意键终止服务！");
            //Console.ReadKey();

            //-----------------------------------------------------标准终结点发布元数据-----------------------------------------------------

            Uri httpBaseAddress = new Uri("http://www.wcf.com/");
            ServiceHost host = new ServiceHost(typeof(CalculatorService), httpBaseAddress);
            host.Description.Behaviors.Add(new ServiceMetadataBehavior());
            host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "");
            //标准终结点发布元数据
            EndpointAddress address = new EndpointAddress("http://www.wcf.com:8000/mex");
            //ServiceMetadataEndpoint  不会使用宿主的基地址
            ServiceEndpoint endpoint = new ServiceMetadataEndpoint(address);
            host.AddServiceEndpoint(endpoint);
            host.Open();

            Console.WriteLine("CalculaorService已经启动，按任意键终止服务！");
            Console.ReadKey();
        }
    }
}
